using System;
using System.Net;

namespace Ausy.Silverlight.VideoPlayer.Common.Configuration
{
    /// <summary>
    /// Cette classe permet de charger la configuration par rapport à une Uri
    /// </summary>
    public class ConfigurationDownloader
    {
        #region public ConfigurationDownloadCompleted Event

        /// <summary>
        /// Evènement lorsque le chargement est terminé
        /// </summary>
        public event EventHandler<ConfigurationDownloadCompletedArgs> ConfigurationDownloadCompleted;

        /// <summary>
        /// Evènement lorsque le chargement est terminé
        /// </summary>
        protected virtual void OnConfigurationDownloadCompleted(ConfigurationDownloadCompletedArgs e)
        {
            EventHandler<ConfigurationDownloadCompletedArgs> handler = ConfigurationDownloadCompleted;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Constructeur
        /// </summary>
        public ConfigurationDownloader()
        {
        }

        #endregion

        #region DownloadConfiguration Methods

        /// <summary>
        /// Cette méthode permet de charger la configuration
        /// </summary>
        /// <param name="filnameUri">Uri du Fichier de configuration</param>
        public void DownloadConfiguration(Uri filnameUri)
        {
            this.DownloadConfiguration(filnameUri, null);
        }

        /// <summary>
        /// Cette méthode permet de charger la configuration
        /// </summary>
        /// <param name="filnameUri">Uri du Fichier de configuration</param>
        /// <param name="userState">objet utilisateur</param>
        public void DownloadConfiguration(Uri filnameUri, object userState)
        {
            if (!filnameUri.Scheme.Equals("file"))
            {
                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.OnDownloadStringCompleted);
                client.DownloadStringAsync(filnameUri, userState);
            }
            else
            {
                this.OnConfigurationDownloadCompleted(new ConfigurationDownloadCompletedArgs(String.Empty, userState, null));
            }
        }

        /// <summary>
        /// Cette méthode permet detraiter la fin du chargement du fichier
        /// </summary>
        /// <param name="sender">Configuration Downloader</param>
        /// <param name="e">Retour de l'appel asynchrone</param>
        private void OnDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                this.OnConfigurationDownloadCompleted(new ConfigurationDownloadCompletedArgs(e.Result, e.UserState, null));
            }
            else
            {
                this.OnConfigurationDownloadCompleted(new ConfigurationDownloadCompletedArgs(String.Empty, e.UserState, e.Error));
            }
        }

        #endregion
    }
}
